﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;
using Visifire.Commons;
using System.Data;

namespace proyecto
{
    public class Vis : AvalonDock.DockablePane
    {
        //Image imagen;
        Byte dock;
        int dataColumn, dataRow;
        int page;
        Chart chart;

        heatMap heatmap;

        DataSeries[] dataSeries;
        DataPoint[] dataPoints;
        int nSeries;
        int table;

        int type;

        int[] groups;

        ScrollViewer scrollingPane;


        bool canceled;

        bool normalized;
        int nSteps;
        double[][] normalizedSeries;
        double[] steps;
        double min, max;
        string[] stepsLabels;

        bool selected;

        bool[] showed;
        bool isNormalized;

        DockPanel config1, config2;

        TextBox text;
        ComboBox combo, comboX, comboY;
        Label label;
        Button but;
        Button configureSeries;

        viewDataTable showTable;

        public AvalonDock.DockableContent aux = new AvalonDock.DockableContent();
        


        /*public Vis(ImageSource source)
        {
            imagen = new Image();
            imagen.Source = source;

            imagen.Height = 200;

            dock = 0;
            AvalonDock.DockableContent aux = new AvalonDock.DockableContent();
            aux.Content = imagen;

            AllowDrop = true;
            
            //DockPanel.SetDock(imagen, Dock.Top);
            
            AddChild(aux);
            
            Background = Brushes.Yellow;
        }*/

        public Vis(data matrix, int type_)
        {

            //CloseCommand = new RoutedUICommand("Window1.tabs[Window1.Tabs1.SelectedIndex].CloseVisualization(this);", "closingVisualization", typeof(string));

            CommandManager.AddPreviewExecutedHandler(this, new ExecutedRoutedEventHandler(ExecuteCommand));

            aux = new AvalonDock.DockableContent();

            scrollingPane = new ScrollViewer();
            scrollingPane.VerticalContentAlignment = VerticalAlignment.Center;
            scrollingPane.HorizontalContentAlignment = HorizontalAlignment.Center;
            

            scrollingPane.Background = Brushes.Transparent;

            scrollingPane.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            

            canceled = false;

            table = 0;

            int n = matrix.getnCols();
            string[] aux1 = new string[n-1];
            
            normalized = false;
            nSteps = 0;

            type = type_;

            for (int i = 1; i<n; i++)
                aux1 [i-1] = matrix.getColumnTittle(0, i);

            selected = false;

            config1 = new DockPanel();
            //config1.Height = 20;
            config2 = new DockPanel();
            //config2.Height = 20;

            text = new TextBox();
            combo = new ComboBox();
            comboX = new ComboBox();
            comboY = new ComboBox();
            label = new Label();
            but = new Button();
            configureSeries = new Button();
            
            //SelectData select = new SelectData(aux1,n-1);

            /*selectDataWindow win = new selectDataWindow(aux1,n-1);

            win.ShowDialog();

            //while (win.getOpen()) { ;}

            if (!win.getSelected())
            {

                dataColumn = 0;
                dataRow = 0;
            }

            else
            {
                //dataColumn = win.getRow();
                dataColumn = win.getColumn();
            }*/


            

            // Create a new instance of Chart
            chart = new Chart();

            

            // Create a new instance of Title
            Title title = new Title();

            // Set title property
            

            // Add title to Titles collection
            chart.Titles.Add(title);

            chart.Width = 400;
            chart.Height = 300;

            //chart.MouseDoubleClick += new MouseButtonEventHandler(chart_MouseDoubleClick);
            chart.MouseRightButtonDown += new MouseButtonEventHandler(chart_MouseRightButtonDown);
            chart.MouseWheel += new MouseWheelEventHandler(chart_MouseWheel);
            chart.MouseLeftButtonDown += new MouseButtonEventHandler(chart_MouseLeftButtonDown);

            // Create a new instance of DataSeries
            //dataSeries = new DataSeries;

            switch (type_){
                case 0:
                    /*Histogram_Design design = new Histogram_Design(aux1, n - 1, matrix.groupsLabels, matrix.ngroups, matrix.nClass);
                    if (!design.ShowDialog().Value) canceled = true;*/

                    if (true)
                    {
                        showed = new bool[matrix.getnRows()];
                        normalized = true;
                        nSteps = 10;
                        steps = new double[nSteps];
                        stepsLabels = new string[nSteps];
                        normalizedSeries = new double[1][];
                        groups = new int[1];
                        nSeries = 1;
                        //dataPoints = new DataPoint[nSeries];

                        for (int i = 0; i < nSeries; i++)
                        {
                            groups[i] = 0;
                        }
                        /*dataColumn = design.getY();
                        dataRow = design.getX();*/

                        dataSeries = new DataSeries[nSeries];
                        //DataPoint dataPoint;

                        int nPoints = 0;
                        int[] nPointInSeries = new int[nSeries];
                        int[] nPointInSteps = new int[nSteps];



                        for (int i = 0; i < nSteps; i++)
                            nPointInSteps[i] = 0;

                        for (int i = 0; i < nSeries; i++)
                        {
                            nPointInSeries[i] = 0;
                        }
                        DataPoint[][] points = new DataPoint[nSeries][];

                        for (int series_cont = 0; series_cont < nSeries; series_cont++)
                        {
                            points[series_cont] = new DataPoint[matrix.getnRows()];

                            dataSeries[series_cont] = new DataSeries();
                            dataSeries[series_cont].RenderAs = RenderAs.Column;
                            for (int i = 0; i < matrix.getnRows(); i++)
                            {
                                bool filt = true;
                                /*if (design.get_Series(series_cont) != -1)
                                {
                                    if (matrix.groups[i][classif] != group)
                                        filt = false;
                                }*/

                                for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                                    if (matrix.getData(0, i, j) < matrix.getMin(j) || matrix.getData(0, i, j) > matrix.getMax(j))
                                    {

                                        //MessageBox.Show("En fila " + i.ToString() + " el dato 
                                        showed[i] = false;
                                        filt = false;
                                    }

                                /*if (matrix.getNormalizedData(i, groups[series_cont]) < matrix.getMin(groups[series_cont]) || matrix.getNormalizedData(i, groups[series_cont]) > matrix.getMax(groups[series_cont]))
                                    filt = false;*/
                                //filt = true;
                                if (filt)
                                {
                                    showed[i] = true;
                                    points[series_cont][nPointInSeries[series_cont]] = new DataPoint();
                                    //points[series_cont][nPointInSeries[series_cont]].XValue = matrix.getNormalizedData(i, design.comboBox1.SelectedIndex);
                                    points[series_cont][nPointInSeries[series_cont]].YValue = matrix.getData(0, i, groups[series_cont]);

                                    if (nPoints == 0)
                                    {
                                        min = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                        max = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                    }

                                    else
                                    {
                                        if (double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString()) < min)
                                            min = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                        if (double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString()) > max)
                                            max = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                    }



                                    nPointInSeries[series_cont]++;
                                    nPoints++;


                                }

                            }

                        }



                        for (int i = 1; i <= nSteps; i++)
                        {
                            steps[i - 1] = min + ((max - min) / nSteps) * i;
                            if (i == 1)
                                stepsLabels[i - 1] = min.ToString() + " - " + steps[i - 1].ToString();
                            else
                                stepsLabels[i - 1] = steps[i - 2].ToString() + " - " + steps[i - 1].ToString();

                        }



                        steps[nSteps - 1] = max;

                        DataPoint[][] pointsToAdd = new DataPoint[nSeries][];

                        for (int i = 0; i < nSeries; i++)
                        {
                            pointsToAdd[i] = new DataPoint[nSteps];
                            for (int j = 0; j < nSteps; j++)
                                pointsToAdd[i][j] = new DataPoint();
                        }



                        for (int i = 0; i < nSeries; i++)
                        {
                            //dataPoints[i] = new DataPoint[nPointInSeries[i]];
                            for (int j = 0; j < nSteps; j++)
                            {
                                pointsToAdd[i][j].AxisXLabel = stepsLabels[j];
                                pointsToAdd[i][j].XValue = j;
                                pointsToAdd[i][j].YValue = 0;
                                nPointInSteps[j] = 0;
                                //dataPoints[i][j] = new DataPoint();
                                //dataPoints[i][j].XValue = j;
                            }

                            for (int j = 0; j < nPointInSeries[i]; j++)
                            {
                                bool foundStep = false;
                                int k;
                                for (k = 0; !foundStep; k++)
                                    if (double.Parse(points[i][j].YValue.ToString()) <= steps[k])
                                        foundStep = true;

                                k--;

                                //pointsToAdd[i][k].YValue = double.Parse(pointsToAdd[i][k].YValue.ToString()) + double.Parse(points[i][j].YValue.ToString());

                                nPointInSteps[k]++;

                                pointsToAdd[i][k].YValue++;
                            }

                            for (int j = 0; j < nSteps; j++)
                            {
                                if (nPointInSteps[j] > 0)
                                {
                                    //pointsToAdd[i][j].YValue = double.Parse(pointsToAdd[i][j].YValue.ToString()) / nPointInSteps[j];
                                    dataSeries[i].DataPoints.Add(pointsToAdd[i][j]);
                                }
                            }

                            dataSeries[i].Name = aux1[groups[i]].Replace('-', '_');
                            chart.Series.Add(dataSeries[i]);
                        }

                        for (int i = 0; i<20; i++){
                            if (matrix.isCreated(i)&&i!=2)
                                combo.Items.Add(matrix.getTableName(i));
                        }

                        label.Content = "Number of Steps";                      

                        but.Click+=new RoutedEventHandler(but_Click);

                        configureSeries.Click += new RoutedEventHandler(configureSeries_Click);

                        combo.Width = 120;
                        label.Width = 120;
                        text.Width = 50;

                        combo.SelectedIndex = 0;

                        combo.SelectionChanged += new SelectionChangedEventHandler(changeTable);

                        but.Content = "OK";

                        configureSeries.Content = "Configure series";

                                            
                        DockPanel.SetDock(configureSeries, Dock.Bottom);
                        config1.Children.Add(configureSeries);
                        DockPanel.SetDock(combo, Dock.Left);
                        config1.Children.Add(combo);
                        DockPanel.SetDock(label, Dock.Left);
                        config1.Children.Add(label);
                        DockPanel.SetDock(text, Dock.Left);
                        config1.Children.Add(text);
                        DockPanel.SetDock(but, Dock.Left);
                        config1.Children.Add(but);
                        

                        DockPanel.SetDock(config1, Dock.Top);

                        config2.Children.Add(config1);

                    }

                    chart.MinHeight = 20;
                    chart.MinWidth = 20;

                    chart.ScrollingEnabled = false;

                    chart.PlotArea.MouseLeftButtonDown += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClicked);
                    chart.PlotArea.MouseLeave += new EventHandler<MouseEventArgs>(chartMouseLeave);
                    chart.PlotArea.MouseLeftButtonUp += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClickedFinish);

                    chart.AnimationEnabled = false;

                    //MessageBox.Show(dataColumn.ToString());

                    dock = 0;
                    
                    
                    //DockPanel LayoutRoot = new DockPanel();

                    //LayoutRoot.Children.Add(chart);

                    config2.LastChildFill = true;
                    config2.Children.Add(chart);

                    scrollingPane.Content = config2;
                    aux.Content = scrollingPane;
                    
                    AllowDrop = true;

                    //DockPanel.SetDock(imagen, Dock.Top);

                    /*AvalonDock.DockableContent config = new AvalonDock.DockableContent();

                    config.Content = config1;
                    
                    AddChild(config);*/

                    AddChild(aux);
                                            

                    break;

                case 1:

                    string fileName = Window1.dat[0].getCluster(0);

                    Image plot = new Image();
                    plot.MouseLeftButtonDown += new MouseButtonEventHandler(plot_MouseLeftButtonDown);
                    plot.MouseRightButtonDown += new MouseButtonEventHandler(plot_MouseRightButtonDown);
                    plot.MouseWheel += new MouseWheelEventHandler(plot_MouseWheel); 
                    plot.Stretch = Stretch.UniformToFill;
                    ImageSourceConverter converter = new ImageSourceConverter();
                    plot.Source = (converter.ConvertFromString(fileName) as ImageSource);

                    config2.Children.Add(plot);
                    scrollingPane.Content = config2; aux.Content = scrollingPane;

                    AddChild(aux);


                    break;


                case 2:
                    Window1.bringProgress();    
                    showed = new bool[matrix.getnRows()];
                    dataPoints = new DataPoint[matrix.getnRows()];
                    groups = new int[matrix.getnRows()];
                    nSeries = 1;
                    for (int i = 0; i < matrix.getnRows(); i++)
                    {
                        groups[i] = 1;
                    }
                    dataColumn = 1;
                    dataRow = 0;

                    dataSeries = new DataSeries[nSeries];
                    DataPoint dataPoint;        

                    for (int series_cont = 0; series_cont < nSeries; series_cont++)
                    {

                        int classif = 0;

                        int group = 0;
                        int acum = 0;

                        bool found_class = false;

                        dataSeries[series_cont] = new DataSeries();
                        dataSeries[series_cont].RenderAs = RenderAs.Column;
                        for (int i = 0; i < matrix.getnRows(); i++)
                        {
                            dataPoints[i] = new DataPoint();
                            dataPoints[i].XValue = matrix.getData(0, i, dataRow);
                            dataPoints[i].YValue = matrix.getData(0, i, dataColumn);
                            try
                            {
                                dataPoints[i].Name = matrix.getRowName(0, i).Replace("-", "_").Replace("/", "_");
                            }
                            catch
                            {
                                dataPoints[i].Name = "_" + matrix.getRowName(0, i).Replace("-", "_").Replace("/", "_");
                            }
                            
                            showed[i] = false;
                            bool filt = true;
                            /*if (groups[series_cont] != -1)
                            {
                                if (matrix.groups[i][classif] != group)
                                    filt = false;
                            }*/

                            for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                                if (matrix.getData(0, i, j) < matrix.getMin(j) || matrix.getData(0, i, j) > matrix.getMax(j))
                                {

                                    //MessageBox.Show("En fila " + i.ToString() + " columna " + j.ToString() + " el dato " + matrix.getData(0, i, j).ToString() + " y filtros " + matrix.getMin(j) + " - " + matrix.getMax(j));

                                    filt = false;
                                }

                            //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))


                            if (filt)
                            {

                                if (normalized) { ;}

                                showed[i] = true;

                                // Create a new instance of DataPoint
                                //dataPoint = new DataPoint();

                                // Set YValue for a DataPoint
                                /*dataPoint.YValue = matrix.getData(0,i, dataColumn);

                                dataPoint.XValue = matrix.getData(0, i, dataRow);*/



                                //MessageBox.Show(dataPoint.YValue.ToString() + " y el maximo es " + matrix.getMax(dataColumn).ToString());
                                
                                // Add dataPoint to DataPoints collection.
                                
                                // new CornerRadius(1.0);
                                /*CornerRadiusConverter converter = new CornerRadiusConverter();
                                dataPoints[i].RadiusY = new CornerRadius(0.2);
                                dataPoints[i].RadiusX = new CornerRadius(0.2);*/
                                //dataPoints[i].MarkerSize = new double();
                                //dataPoints[i].MarkerSize = 3;
                                dataSeries[series_cont].DataPoints.Add(dataPoints[i]);
                                
                                //MessageBox.Show("added row " + i.ToString() + " with value " + matrix.getData(i, dataColumn).ToString() + "," + matrix.getData(i, dataRow));
                            }
                        }
                        //chart.CornerRadius = new CornerRadius(0.2);        
                        dataSeries[series_cont].RenderAs = RenderAs.Point;
                        //dataSeries[series_cont].Color = Brushes.Black;
                        
                        
                        

                        //MessageBox.Show(dataSeries[series_cont].RenderSize.ToString());
                                                                        
                        chart.Series.Add(dataSeries[series_cont]);

                       
      
                    }

                    

                    chart.MinHeight = 20;
                    chart.MinWidth = 20;
                    
                    chart.ScrollingEnabled = false;

                    chart.PlotArea.MouseLeftButtonDown += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClicked);
                    chart.PlotArea.MouseLeave += new EventHandler<MouseEventArgs>(chartMouseLeave);
                    chart.PlotArea.MouseLeftButtonUp += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClickedFinish);

                    chart.AnimationEnabled = false;

                    //MessageBox.Show(dataColumn.ToString());

                    dock = 0;
                    

                    //DockPanel LayoutRoot = new DockPanel();

                    //LayoutRoot.Children.Add(chart);

                    for (int i = 0; i<20; i++){
                        if (matrix.isCreated(i)&&i!=2)
                        {
                            combo.Items.Add(matrix.getTableName(i));
                                
                        }
                    }
   
                    for (int i = 1; i < matrix.getnCols(); i++)
                    {
                        comboX.Items.Add(matrix.getColumnTittle(table,i));
                        comboY.Items.Add(matrix.getColumnTittle(table, i));
                    }

                    configureSeries.Click += new RoutedEventHandler(configureSeries_Click);

                    combo.Width = 120;
                    comboX.Width = 120;
                    comboY.Width = 120;

                    combo.SelectedIndex = 0;
                    comboX.SelectedIndex = 0;
                    comboY.SelectedIndex = 1;

                    combo.SelectionChanged += new SelectionChangedEventHandler(changeTable);
                    comboX.SelectionChanged += new SelectionChangedEventHandler(changeAxys);
                    comboY.SelectionChanged += new SelectionChangedEventHandler(changeAxys);

                    

                    //////////////////////
                    configureSeries.Content = "Configure series";
                             
                    DockPanel.SetDock(configureSeries, Dock.Bottom);
                    config1.Children.Add(configureSeries);
                    DockPanel.SetDock(combo, Dock.Left);
                    config1.Children.Add(combo);
                    DockPanel.SetDock(comboX, Dock.Top);
                    config1.Children.Add(comboX);
                    DockPanel.SetDock(comboY, Dock.Top);
                    config1.Children.Add(comboY);
                        
                    DockPanel.SetDock(config1, Dock.Top);

                    config2.Children.Add(config1);

                    config2.LastChildFill = true;
                    config2.Children.Add(chart);

                    scrollingPane.Content = config2; aux.Content = scrollingPane;
                    
                    AllowDrop = true;

                    //DockPanel.SetDock(imagen, Dock.Top);

                    /*AvalonDock.DockableContent config = new AvalonDock.DockableContent();

                    config.Content = config1;
                    
                    AddChild(config);*/

                    AddChild(aux);


                    break;
                case 5:

                    /*LineChart_Design designLine = new LineChart_Design(matrix);
                    
                    if (!designLine.ShowDialog().Value) canceled = true;
                    else
                    {*/
                        nSeries = 1;
                        dataSeries = new DataSeries[matrix.getnRows()];
                        groups = new int[nSeries];
                        int seriesCont = 0;
                        showed = new bool[matrix.getnRows()];
                        for (int i = 0; i < matrix.getnRows(); i++)
                            if (i == 0)
                            {
                                groups[seriesCont] = i;
                                dataSeries[seriesCont] = new DataSeries();
                                dataSeries[seriesCont].Name = matrix.getRow(0, i)[0].ToString().Replace('-', '_');
                                dataSeries[i].ToolTipText = dataSeries[i].Name;
                                for (int j = 0; j < matrix.getnCols() - 1; j++)
                                {
                                    DataPoint newPoint = new DataPoint();
                                    newPoint.XValue = j;
                                    newPoint.AxisXLabel = matrix.getColumnTittle(0, j + 1);
                                    newPoint.YValue = matrix.getData(0, i, j);
                                    newPoint.ToolTipText = dataSeries[i].ToolTipText + ": " + newPoint.AxisXLabel.ToString() + " , " + newPoint.YValue.ToString();
                                    dataSeries[seriesCont].DataPoints.Add(newPoint);
                                }
                                dataSeries[seriesCont].RenderAs = RenderAs.Line;
                                
                                if (matrix.isInFilterRange[i])
                                {
                                    showed[i] = true;

                                    chart.Series.Add(dataSeries[seriesCont]);
                                }
                                else
                                    showed[i] = false;
                                seriesCont++;
                                dataSeries[i].ShowInLegend = false;
                                //dataSeries[i].LabelText = dataSeries[i].Name;
                                //dataSeries[i].LabelEnabled = true;
                                
                                //dataSeries[i].
                            }
                            else
                            {
                                dataSeries[i] = new DataSeries();
                                dataSeries[i].Name = "_" + matrix.getRow(0, i)[0].ToString().Replace('-', '_');
                                dataSeries[i].ToolTipText = dataSeries[i].Name;
                                for (int j = 0; j < matrix.getnCols() - 1; j++)
                                {
                                    DataPoint newPoint = new DataPoint();
                                    newPoint.XValue = j;
                                    newPoint.AxisXLabel = matrix.getColumnTittle(0, j + 1);
                                    newPoint.YValue = matrix.getData(0, i, j);
                                    newPoint.ToolTipText = dataSeries[i].ToolTipText + ": " + newPoint.AxisXLabel.ToString() + " , " + newPoint.YValue.ToString();
                                    dataSeries[i].DataPoints.Add(newPoint);
                                }
                                dataSeries[i].RenderAs = RenderAs.Line;
                                

                                dataSeries[i].ShowInLegend = false;
                                //dataSeries[i].LabelText = dataSeries[i].Name;
                                //dataSeries[i].LabelEnabled = true;
                                
                                

                                showed[i] = false;
                            }

                    //}
                    
                    /*LineChart_Design design_ = new LineChart_Design(aux1, n - 1, matrix.groupsLabels, matrix.ngroups, matrix.nClass);
                    if (!design.ShowDialog().Value) canceled = true;
                    else
                    {


                        if (design.checkBox1.IsChecked.Value)
                        {
                            normalized = true;
                            nSteps = (int.Parse(design.textBox1.Text));
                            steps = new double[nSteps];
                            stepsLabels = new string[nSteps];
                            normalizedSeries = new double[design.get_nSeries()][];
                            groups = new int[design.get_nSeries()];
                            nSeries = design.get_nSeries();
                            for (int i = 0; i < nSeries; i++)
                            {
                                groups[i] = design.get_Series(i);
                            }
                            dataColumn = design.getY();
                            dataRow = design.getX();

                            dataSeries = new DataSeries[nSeries];
                            //DataPoint dataPoint;

                            int nPoints = 0;
                            int [] nPointInSeries = new int [nSeries];
                            int [] nPointInSteps = new int [nSteps];

                            

                            for (int i = 0; i<nSteps; i++)
                                nPointInSteps[i] = 0;

                            for (int i = 0; i<nSeries; i++){
                                nPointInSeries[i]=0;
                            }
                            DataPoint [][] points = new DataPoint[nSeries][];

                            for (int series_cont = 0; series_cont < nSeries; series_cont++)
                            {
                                points[series_cont] = new DataPoint [matrix.getnRows()];

                                int classif = 0;

                                int group = 0;
                                int acum = 0;

                                bool found_class = false;
                                if (design.get_Series(series_cont) != -1)
                                {
                                    
                                    for (int i = 0; !found_class; i++)
                                    {
                                        if (design.get_Series(series_cont) < acum + matrix.ngroups[i])
                                        {
                                            found_class = true;
                                            classif = i;
                                        }
                                        else
                                            acum = acum + matrix.ngroups[i];
                                    }

                                    group = design.get_Series(series_cont) - (acum);
                                    
                                    //MessageBox.Show(classif.ToString() + " " + group.ToString());


                                }



                                dataSeries[series_cont] = new DataSeries();
                                dataSeries[series_cont].RenderAs = RenderAs.Column;
                                for (int i = 0; i < matrix.getnRows(); i++)
                                {
                                    bool filt = true;
                                    if (design.get_Series(series_cont) != -1)
                                    {
                                        if (matrix.groups[i][classif] != group)
                                            filt = false;
                                    }

                                    for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                                        if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                                        {

                                            //MessageBox.Show("En fila " + i.ToString() + " el dato 

                                            filt = false;
                                        }

                                    if (filt)
                                    {
                                        
                                        points[series_cont][nPointInSeries[series_cont]] = new DataPoint();
                                        points[series_cont][nPointInSeries[series_cont]].XValue = matrix.getData(i, design.comboBox1.SelectedIndex);
                                        points[series_cont][nPointInSeries[series_cont]].YValue = matrix.getData(i, design.comboBox2.SelectedIndex);
                                        
                                        if (nPoints == 0)
                                        {
                                            min = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                            max = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                        }

                                        else
                                        {
                                            if (double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString()) < min)
                                                min = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                            if (double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString()) > max)
                                                max = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                        }

                                        

                                        nPointInSeries[series_cont]++;
                                        nPoints++;


                                    }

                                }
                                
                            }

                            
                            
                            for (int i = 1; i <= nSteps; i++)
                            {
                                steps[i - 1] = min + ((max - min) / nSteps) * i;
                                if (i == 1)
                                    stepsLabels[i - 1] = i.ToString() + ": " + min.ToString() + " - " + steps[i - 1].ToString();
                                else
                                    stepsLabels[i - 1] = i.ToString() + ": " + steps[i - 2].ToString() + " - " + steps[i - 1].ToString();

                            }



                            steps[nSteps - 1] = max;
                            
                            DataPoint[][] pointsToAdd = new DataPoint[nSeries][];

                            for (int i = 0; i < nSeries; i++)
                            {
                                pointsToAdd[i] = new DataPoint[nSteps];
                                for (int j = 0; j < nSteps; j++)
                                    pointsToAdd[i][j] = new DataPoint();
                            }


                            
                            for (int i = 0; i < nSeries; i++)
                            {
                                for (int j = 0; j < nSteps; j++)
                                {
                                    pointsToAdd[i][j].AxisXLabel = stepsLabels[j];
                                    pointsToAdd[i][j].XValue = j;
                                    pointsToAdd[i][j].YValue = 0;
                                    nPointInSteps[j] = 0;
                                }
                                
                                for (int j = 0; j < nPointInSeries[i]; j++)
                                {
                                    bool foundStep = false;
                                    int k;
                                    for (k = 0; !foundStep; k++)
                                        if (double.Parse(points[i][j].XValue.ToString()) <= steps[k])
                                            foundStep = true;

                                    k--;

                                    pointsToAdd[i][k].YValue = double.Parse(pointsToAdd[i][k].YValue.ToString()) + double.Parse(points[i][j].YValue.ToString());

                                    nPointInSteps[k]++;
                                }
                                
                                for (int j = 0; j < nSteps; j++)
                                {
                                    if (nPointInSteps[j] > 0)
                                    {
                                        pointsToAdd[i][j].YValue = double.Parse(pointsToAdd[i][j].YValue.ToString()) / nPointInSteps[j];
                                        dataSeries[i].DataPoints.Add(pointsToAdd[i][j]);
                                    }
                                }
                                switch (type)
                                {
                                    case 0:
                                        
                                        break;
                                    case 2:
                                        dataSeries[i].RenderAs = RenderAs.Point;
                                        break;
                                    case 5:
                                        dataSeries[i].RenderAs = RenderAs.Line;
                                        break;
                                }

                                chart.Series.Add(dataSeries[i]);
                            }

                        }


                        else
                        {
                            
                            groups = new int[design.get_nSeries()];
                            nSeries = design.get_nSeries();
                            for (int i = 0; i < nSeries; i++)
                            {
                                groups[i] = design.get_Series(i);
                            }
                            dataColumn = design.getY();
                            dataRow = design.getX();

                            dataSeries = new DataSeries[nSeries];
                            DataPoint dataPoint;






                            for (int series_cont = 0; series_cont < design.get_nSeries(); series_cont++)
                            {




                                int classif = 0;

                                int group = 0;
                                int acum = 0;

                                bool found_class = false;
                                if (design.get_Series(series_cont) != -1)
                                {
                                    for (int i = 0; !found_class; i++)
                                    {
                                        if (design.get_Series(series_cont) < acum + matrix.ngroups[i])
                                        {
                                            found_class = true;
                                            classif = i;
                                        }
                                        else
                                            acum = acum + matrix.ngroups[i];
                                    }

                                    group = design.get_Series(series_cont) - (acum);

                                    //MessageBox.Show(classif.ToString() + " " + group.ToString());


                                }



                                dataSeries[series_cont] = new DataSeries();
                                dataSeries[series_cont].RenderAs = RenderAs.Column;
                                for (int i = 0; i < matrix.getnRows(); i++)
                                {
                                    bool filt = true;
                                    if (design.get_Series(series_cont) != -1)
                                    {
                                        if (matrix.groups[i][classif] != group)
                                            filt = false;
                                    }

                                    for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                                        if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                                        {


                                            MessageBox.Show("En fila " + i.ToString() + " columna " + j.ToString() + " el dato " + matrix.getData(i, j).ToString() + " y filtros " + matrix.getMin(j) + " - " + matrix.getMax(j));

                                            filt = false;
                                        }



                                    //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))


                                    if (filt)
                                    {

                                        if (normalized) { ;}





                                        // Create a new instance of DataPoint
                                        dataPoint = new DataPoint();

                                        // Set YValue for a DataPoint
                                        dataPoint.YValue = matrix.getData(i, dataColumn);

                                        dataPoint.XValue = matrix.getData(i, dataRow);

                                        //MessageBox.Show(dataPoint.YValue.ToString() + " y el maximo es " + matrix.getMax(dataColumn).ToString());


                                        // Add dataPoint to DataPoints collection.
                                        dataSeries[series_cont].DataPoints.Add(dataPoint);
                                        //MessageBox.Show("added row " + i.ToString() + " with value " + matrix.getData(i, dataColumn).ToString() + "," + matrix.getData(i, dataRow));
                                    }
                                }
                                switch (type)
                                {
                                    case 0:
                                        
                                        break;
                                    case 2:
                                        dataSeries[series_cont].RenderAs = RenderAs.Point;
                                        break;
                                    case 5:
                                        dataSeries[series_cont].RenderAs = RenderAs.Line;
                                        break;
                                }
                                chart.Series.Add(dataSeries[series_cont]);
                            }
                        }
                    }













                    
                    break;
                case 1:
                    
                    break;
                /*case 2:

                    Histogram_Design design_2 = new Histogram_Design(aux1, n - 1, matrix.groupsLabels, matrix.ngroups, matrix.nClass);
                    if (!design_2.ShowDialog().Value) canceled = true;
                    else
                    {

                        if (design_2.checkBox1.IsChecked.Value)
                        {
                            normalized = true;
                            nSteps = (int.Parse(design_2.textBox1.Text));
                            steps = new double[nSteps];
                            stepsLabels = new string[nSteps];
                            normalizedSeries = new double[design_2.get_nSeries()][];
                            groups = new int[design_2.get_nSeries()];
                            nSeries = design.get_nSeries();
                            for (int i = 0; i < nSeries; i++)
                            {
                                groups[i] = design_2.get_Series(i);
                            }
                            dataColumn = design_2.getY();
                            dataRow = design_2.getX();

                            dataSeries = new DataSeries[nSeries];
                            //DataPoint dataPoint;

                            int nPoints = 0;
                            int[] nPointInSeries = new int[nSeries];
                            int[] nPointInSteps = new int[nSteps];



                            for (int i = 0; i < nSteps; i++)
                                nPointInSteps[i] = 0;

                            for (int i = 0; i < nSeries; i++)
                            {
                                nPointInSeries[i] = 0;
                            }
                            DataPoint[][] points = new DataPoint[nSeries][];

                            for (int series_cont = 0; series_cont < nSeries; series_cont++)
                            {
                                points[series_cont] = new DataPoint[matrix.getnRows()];

                                int classif = 0;

                                int group = 0;
                                int acum = 0;

                                bool found_class = false;
                                if (design.get_Series(series_cont) != -1)
                                {

                                    for (int i = 0; !found_class; i++)
                                    {
                                        if (design.get_Series(series_cont) < acum + matrix.ngroups[i])
                                        {
                                            found_class = true;
                                            classif = i;
                                        }
                                        else
                                            acum = acum + matrix.ngroups[i];
                                    }

                                    group = design.get_Series(series_cont) - (acum);

                                    //MessageBox.Show(classif.ToString() + " " + group.ToString());


                                }



                                dataSeries[series_cont] = new DataSeries();
                                dataSeries[series_cont].RenderAs = RenderAs.Column;
                                for (int i = 0; i < matrix.getnRows(); i++)
                                {
                                    bool filt = true;
                                    if (design.get_Series(series_cont) != -1)
                                    {
                                        if (matrix.groups[i][classif] != group)
                                            filt = false;
                                    }

                                    for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                                        if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                                        {

                                            //MessageBox.Show("En fila " + i.ToString() + " el dato 

                                            filt = false;
                                        }

                                    if (filt)
                                    {

                                        points[series_cont][nPointInSeries[series_cont]] = new DataPoint();
                                        points[series_cont][nPointInSeries[series_cont]].XValue = matrix.getData(i, design.comboBox1.SelectedIndex);
                                        points[series_cont][nPointInSeries[series_cont]].YValue = matrix.getData(i, design.comboBox2.SelectedIndex);

                                        if (nPoints == 0)
                                        {
                                            min = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                            max = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                        }

                                        else
                                        {
                                            if (double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString()) < min)
                                                min = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                            if (double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString()) > max)
                                                max = double.Parse(points[series_cont][nPointInSeries[series_cont]].XValue.ToString());
                                        }



                                        nPointInSeries[series_cont]++;
                                        nPoints++;


                                    }

                                }

                            }



                            for (int i = 1; i <= nSteps; i++)
                            {
                                steps[i - 1] = min + ((max - min) / nSteps) * i;
                                if (i == 1)
                                    stepsLabels[i - 1] = i.ToString() + ": " + min.ToString() + " - " + steps[i - 1].ToString();
                                else
                                    stepsLabels[i - 1] = i.ToString() + ": " + steps[i - 2].ToString() + " - " + steps[i - 1].ToString();

                            }



                            steps[nSteps - 1] = max;

                            DataPoint[][] pointsToAdd = new DataPoint[nSeries][];

                            for (int i = 0; i < nSeries; i++)
                            {
                                pointsToAdd[i] = new DataPoint[nSteps];
                                for (int j = 0; j < nSteps; j++)
                                    pointsToAdd[i][j] = new DataPoint();
                            }



                            for (int i = 0; i < nSeries; i++)
                            {
                                for (int j = 0; j < nSteps; j++)
                                {
                                    pointsToAdd[i][j].AxisXLabel = stepsLabels[j];
                                    pointsToAdd[i][j].XValue = j;
                                    pointsToAdd[i][j].YValue = 0;
                                    nPointInSteps[j] = 0;
                                }

                                for (int j = 0; j < nPointInSeries[i]; j++)
                                {
                                    bool foundStep = false;
                                    int k;
                                    for (k = 0; !foundStep; k++)
                                        if (double.Parse(points[i][j].XValue.ToString()) <= steps[k])
                                            foundStep = true;

                                    k--;

                                    pointsToAdd[i][k].YValue = double.Parse(pointsToAdd[i][k].YValue.ToString()) + double.Parse(points[i][j].YValue.ToString());

                                    nPointInSteps[k]++;
                                }

                                for (int j = 0; j < nSteps; j++)
                                {
                                    if (nPointInSteps[j] > 0)
                                    {
                                        pointsToAdd[i][j].YValue = double.Parse(pointsToAdd[i][j].YValue.ToString()) / nPointInSteps[j];
                                        dataSeries[i].DataPoints.Add(pointsToAdd[i][j]);
                                    }
                                }
                                dataSeries[i].RenderAs = RenderAs.Point;
                                chart.Series.Add(dataSeries[i]);
                            }

                        }


                        else
                        {

                            groups = new int[design_2.get_nSeries()];
                            nSeries = design_2.get_nSeries();
                            for (int i = 0; i < nSeries; i++)
                            {
                                groups[i] = design_2.get_Series(i);
                            }
                            dataColumn = design_2.getY();
                            dataRow = design_2.getX();

                            dataSeries = new DataSeries[nSeries];
                            DataPoint dataPoint;

                            for (int series_cont = 0; series_cont < design_2.get_nSeries(); series_cont++)
                            {

                                int classif = 0;

                                int group;
                                int acum = 0;

                                bool found_class = false;
                                for (int i = 0; !found_class; i++)
                                {
                                    if (design_2.get_Series(series_cont) < acum + matrix.ngroups[i])
                                    {
                                        found_class = true;
                                        classif = i;
                                    }
                                    else
                                        acum = acum + matrix.ngroups[i];
                                }

                                group = design_2.get_Series(series_cont) - (acum);

                                //MessageBox.Show(classif.ToString() + " " + group.ToString());



                                dataSeries[series_cont] = new DataSeries();
                                dataSeries[series_cont].RenderAs = RenderAs.Column;
                                for (int i = 0; i < matrix.getnRows(); i++)
                                {
                                    bool filt = true;
                                    if (design_2.get_Series(series_cont) != -1)
                                    {
                                        if (matrix.groups[i][classif] != group)
                                            filt = false;
                                    }

                                    for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                                        if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                                        {

                                            //MessageBox.Show("En fila " + i.ToString() + " el dato 

                                            filt = false;
                                        }



                                    //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))


                                    if (filt)
                                    {




                                        // Create a new instance of DataPoint
                                        dataPoint = new DataPoint();

                                        // Set YValue for a DataPoint
                                        dataPoint.YValue = matrix.getData(i, dataColumn);

                                        dataPoint.XValue = matrix.getData(i, dataRow);

                                        //MessageBox.Show(dataPoint.YValue.ToString() + " y el maximo es " + matrix.getMax(dataColumn).ToString());


                                        // Add dataPoint to DataPoints collection.
                                        dataSeries[series_cont].DataPoints.Add(dataPoint);
                                        //MessageBox.Show("added row " + i.ToString() + " with value " + matrix.getData(i, dataColumn).ToString() + "," + matrix.getData(i, dataRow));
                                    }
                                }
                                dataSeries[series_cont].RenderAs = RenderAs.Point;
                                chart.Series.Add(dataSeries[series_cont]);
                            }
                        }
                    }

                    //dataSeries.RenderAs = RenderAs.Point;*/
                    chart.MinHeight = 20;
                    chart.MinWidth = 20;

                    chart.ScrollingEnabled = false;

                    chart.PlotArea.MouseLeftButtonDown += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClicked);
                    chart.PlotArea.MouseLeave += new EventHandler<MouseEventArgs>(chartMouseLeave);
                    chart.PlotArea.MouseLeftButtonUp += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClickedFinish);

                    chart.AnimationEnabled = false;

                    

                    //MessageBox.Show(dataColumn.ToString());

                    dock = 0;
                    

                    //DockPanel LayoutRoot = new DockPanel();

                    //LayoutRoot.Children.Add(chart);

                    /*config2.LastChildFill = true;
                    config2.Children.Add(chart);

                    scrollingPane.Content = config2; aux.Content = scrollingPane;*/
                    
                    AllowDrop = true;

                    //DockPanel.SetDock(imagen, Dock.Top);

                    /*AvalonDock.DockableContent config = new AvalonDock.DockableContent();

                    config.Content = config1;
                    
                    AddChild(config);*/

                    /*label.Content = "Number of Steps";



                    but.Click += new RoutedEventHandler(but_Click);*/

                    configureSeries.Click += new RoutedEventHandler(configureSeries_Click);
                    /*
                    combo.Width = 120;
                    label.Width = 120;
                    text.Width = 50;

                    combo.SelectedIndex = 0;

                    combo.SelectionChanged += new SelectionChangedEventHandler(changeTable);

                    but.Content = "OK";*/

                    
                    configureSeries.Content = "Configure series";

                    for (int i = 0; i < 20; i++)
                    {
                        if (matrix.isCreated(i)&&i!=2)
                        {
                            combo.Items.Add(matrix.getTableName(i));

                        }
                    }


                    combo.SelectedIndex = 0;

                    combo.SelectionChanged += new SelectionChangedEventHandler(changeTable);


                    DockPanel.SetDock(combo, Dock.Left);
                    config1.Children.Add(combo);
                    DockPanel.SetDock(configureSeries, Dock.Right);
                    config1.Children.Add(configureSeries);
                    /*DockPanel.SetDock(combo, Dock.Left);
                    config1.Children.Add(combo);
                    DockPanel.SetDock(label, Dock.Left);
                    config1.Children.Add(label);
                    DockPanel.SetDock(text, Dock.Left);
                    config1.Children.Add(text);
                    DockPanel.SetDock(but, Dock.Left);
                    config1.Children.Add(but);*/


                    DockPanel.SetDock(config1, Dock.Top);

                    config2.Children.Add(config1);

                    config2.Children.Add(chart);

                    scrollingPane.Content = config2; aux.Content = scrollingPane;


                    AddChild(aux);

                    break;
                case 3:

                    heatmap = new heatMap(matrix.getTable(2));


                    config2.LastChildFill = true;
                    config2.Children.Add(heatmap);

                    scrollingPane.Content = config2; aux.Content = scrollingPane;

                    AllowDrop = true;

                    AddChild(aux);

                    break;
                case 4:

                    /*BoxPlott_Design designBox = new BoxPlott_Design(aux1, n - 1, matrix.groupsLabels, matrix.ngroups, matrix.nClass);
                    showed = new bool[matrix.getnRows()];
                    if (!designBox.ShowDialog().Value)
                        canceled = true;
                    else
                    {*/
                        showed = new bool[matrix.getnRows()];
                        nSeries = 1;
                        dataSeries = new DataSeries[nSeries];
                        groups = new int[nSeries];
                        for (int i = 0; i < nSeries; i++)
                        {
                            groups[i] = -1;
                            dataSeries[i] = new DataSeries();

                            if (groups[i] != -1)
                            {
                                int classif = 0;

                                int group = 0;
                                int acum = 0;

                                bool found_class = false;


                                for (int j = 0; !found_class; j++)
                                {
                                    if (groups[i] < acum + matrix.ngroups[j])
                                    {
                                        found_class = true;
                                        classif = j;
                                    }
                                    else
                                        acum = acum + matrix.ngroups[j];
                                }
                                group = groups[i] - (acum);

                                for (int j = 0; j < matrix.getnCols() - 1; j++)
                                {
                                    if (matrix.groups[j][classif] == group)
                                    {
                                        DataPoint newPoint = new DataPoint();
                                        newPoint.XValue = j;
                                        newPoint.AxisXLabel = matrix.getColumnTittle(0, j +1);
                                        double[] YValues = new double[matrix.getnRows()];
                                        int nValues = 0;
                                        for (int l = 0; l < matrix.getnRows(); l++)
                                            if (matrix.isInFilterRange[l])
                                            {
                                                YValues[nValues] = matrix.getData(0, l, j);
                                                for (int k = nValues; k>0; k--)
                                                    if (YValues[k]<YValues[k-1]){
                                                        double a = YValues[k-1];
                                                        YValues[k-1]=YValues[k];
                                                        YValues[k] = a;
                                                    }
                                                nValues++;
                                                showed[l] = true;
                                            }
                                            else
                                                showed[i] = false;

                                        int per10 = nValues / 10;
                                        int per90 = (nValues * 90) / 100;
                                        int per25 = (nValues * 25) / 100;
                                        int per75 = (nValues * 75) / 100;

                                        double[] Y = new double[] { YValues[per25], YValues[per75], YValues[per10], YValues[per90] };

                                        newPoint.YValues = Y;
                                        dataSeries[i].DataPoints.Add(newPoint);
                                    }
                                }
                            }

                            else
                            {
                                for (int j = 0; j < matrix.getnCols() - 1; j++)
                                {

                                    DataPoint newPoint = new DataPoint();
                                    newPoint.XValue = j;
                                    newPoint.AxisXLabel = matrix.getColumnTittle(0, j + 1);
                                    double[] YValues = new double[matrix.getnRows()];
                                    int nValues = 0;
                                    for (int l = 0; l < matrix.getnRows(); l++)
                                        if (matrix.isInFilterRange[l])
                                        {
                                            YValues[nValues] = matrix.getData(0, l, j);
                                            for (int k = nValues; k>0; k--)
                                                if (YValues[k]<YValues[k-1]){
                                                    double a = YValues[k-1];
                                                    YValues[k-1]=YValues[k];
                                                    YValues[k] = a;
                                                }
                                            nValues++;
                                            showed[l] = true;
                                        }
                                        else
                                            showed[i] = false;

                                    int per10 = nValues / 10;
                                    int per90 = (nValues * 90) / 100;
                                    int per25 = (nValues * 25) / 100;
                                    int per75 = (nValues * 75) / 100;

                                    double[] Y = new double[] { YValues[per25], YValues[per75], YValues[per10], YValues[per90] };
                                    
                                    newPoint.YValues = Y;
                                    dataSeries[i].DataPoints.Add(newPoint);

                                }
                            }

                            dataSeries[i].RenderAs = RenderAs.CandleStick;
                            dataSeries[i].Name = "_"+("All_groups");                        chart.Series.Add(dataSeries[i]);


                        }
                    chart.MinHeight = 20;
                    chart.MinWidth = 20;

                    chart.ScrollingEnabled = false;

                    chart.PlotArea.MouseLeftButtonDown += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClicked);
                    chart.PlotArea.MouseLeave += new EventHandler<MouseEventArgs>(chartMouseLeave);
                    chart.PlotArea.MouseLeftButtonUp += new EventHandler<PlotAreaMouseButtonEventArgs>(chartClickedFinish);

                    chart.AnimationEnabled = false;

                    combo = new ComboBox();

                    for (int i = 0; i < 20; i++)
                    {
                        if (matrix.isCreated(i)&&i!=2)
                        {
                            combo.Items.Add(matrix.getTableName(i));

                        }
                    }

                    combo.SelectedIndex = 0;

                    combo.SelectionChanged += new SelectionChangedEventHandler(changeTable);

                    configureSeries = new Button();
                    configureSeries.Content = "Configure series";
                    configureSeries.Click += new RoutedEventHandler(configureSeries_Click);

                    DockPanel.SetDock(combo, Dock.Left);
                    DockPanel.SetDock(configureSeries, Dock.Left);

                    config1.Children.Add(combo);
                    config1.Children.Add(configureSeries);

                    DockPanel.SetDock(config1, Dock.Top);

                    config2.Children.Add(config1);

                    //MessageBox.Show(dataColumn.ToString());

                    dock = 0;
                    

                    //DockPanel LayoutRoot = new DockPanel();

                    //LayoutRoot.Children.Add(chart);

                    config2.LastChildFill = true;
                    config2.Children.Add(chart);

                    scrollingPane.Content = config2; aux.Content = scrollingPane;
                    
                    AllowDrop = true;

                    //DockPanel.SetDock(imagen, Dock.Top);

                    /*AvalonDock.DockableContent config = new AvalonDock.DockableContent();

                    config.Content = config1;
                    
                    AddChild(config);*/

                    AddChild(aux);
                    


                                   

                                



                    //dataSeries.RenderAs = RenderAs.CandleStick;
                    break;


                case 6: //HeatMap for any table

                    DataTable auxTable = matrix.getTable(table).Clone();

                    for (int i = 0; i < matrix.getnRows(); i++)
                    {
                        if (matrix.isInFilterRange[i])
                        {
                            DataRow row = auxTable.NewRow();
                            row[0] = matrix.getRowName(table, i);

                            for (int j = 1; j < matrix.getnCols(); j++)
                                row[j] = matrix.getData(table, i, j-1) / matrix.getMax(j - 1);

                            auxTable.Rows.Add(row);
                        }
                    }

                    heatmap = new heatMap(auxTable, true);

                    aux.Content = heatmap;

                    AddChild(aux);

                    break;

                case 10: //show table

                    selectTable select = new selectTable(matrix);

                    select.ShowDialog();

                    if (!select.result()) return;

                    string name = select.tableName();

                    table = 0;

                    for (int i = 0; i < 20; i++)
                        if (matrix.isCreated(i))
                            if (matrix.getTable(i).TableName == name)
                                table = i;

                    showTable = new viewDataTable(matrix.getTable(table));

                    combo = new ComboBox();

                    for (int i = 0; i < 20; i++)
                    {
                        if (matrix.isCreated(i) && i != 2)
                        {
                            combo.Items.Add(matrix.getTableName(i));

                        }
                    }

                    combo.SelectedIndex = 0;

                    combo.SelectionChanged += new SelectionChangedEventHandler(changeTable);

                    config1.Children.Add(combo);

                    DockPanel.SetDock(combo, Dock.Top);

                    config1.Children.Add(showTable);

                    aux.Content = config1;

                    AddChild(aux);

                    break;





                /*case 5:
                    Histogram_Design design_5 = new Histogram_Design(aux1, n - 1, matrix.groupsLabels, matrix.ngroups, matrix.nClass);
                    if (!design_5.ShowDialog().Value) canceled = true;
                    else
                    {
                        groups = new int[design_5.get_nSeries()];
                        nSeries = design_5.get_nSeries();
                        for (int i = 0; i < nSeries; i++)
                        {
                            groups[i] = design_5.get_Series(i);
                        }
                        dataColumn = design_5.getY();
                        dataRow = design_5.getX();

                        dataSeries = new DataSeries[nSeries];
                        DataPoint dataPoint;

                        for (int series_cont = 0; series_cont < design_5.get_nSeries(); series_cont++)
                        {

                            int classif = 0;

                            int group;
                            int acum = 0;

                            bool found_class = false;
                            for (int i = 0; !found_class; i++)
                            {
                                if (design_5.get_Series(series_cont) < acum + matrix.ngroups[i])
                                {
                                    found_class = true;
                                    classif = i;
                                }
                                else
                                    acum = acum + matrix.ngroups[i];
                            }

                            group = design_5.get_Series(series_cont) - (acum);

                            //MessageBox.Show(classif.ToString() + " " + group.ToString());



                            dataSeries[series_cont] = new DataSeries();
                            dataSeries[series_cont].RenderAs = RenderAs.Column;
                            for (int i = 0; i < matrix.getnRows(); i++)
                            {
                                bool filt = true;
                                if (design_5.get_Series(series_cont) != -1)
                                {
                                    if (matrix.groups[i][classif] != group)
                                        filt = false;
                                }

                                for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                                    if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                                    {

                                        //MessageBox.Show("En fila " + i.ToString() + " el dato 

                                        filt = false;
                                    }



                                //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))


                                if (filt)
                                {




                                    // Create a new instance of DataPoint
                                    dataPoint = new DataPoint();

                                    // Set YValue for a DataPoint
                                    dataPoint.YValue = matrix.getData(i, dataColumn);

                                    dataPoint.XValue = matrix.getData(i, dataRow);

                                    //MessageBox.Show(dataPoint.YValue.ToString() + " y el maximo es " + matrix.getMax(dataColumn).ToString());


                                    // Add dataPoint to DataPoints collection.
                                    dataSeries[series_cont].DataPoints.Add(dataPoint);
                                    //MessageBox.Show("added row " + i.ToString() + " with value " + matrix.getData(i, dataColumn).ToString() + "," + matrix.getData(i, dataRow));
                                }
                            }
                            dataSeries[series_cont].RenderAs = RenderAs.Line;
                            chart.Series.Add(dataSeries[series_cont]);
                        }
                    }
                    break;
                default:
                    break;*/
            }



            //dataSeries.RenderAs = RenderAs.Point

            // Set DataSeries property
            

            // Create a DataPoint

            /*
            DataPoint dataPoint;

            for (int i = 0; i < matrix.getnRows(); i++)
            {
                bool filt = true;

                for (int j = 0; j < matrix.getnCols()-1 && filt; j++)

                    if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                    {

                        //MessageBox.Show("En fila " + i.ToString() + " el dato 

                        filt = false;
                    }



                //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))


                if (filt)
                {

                    // Create a new instance of DataPoint
                    dataPoint = new DataPoint();

                    // Set YValue for a DataPoint
                    dataPoint.YValue = matrix.getData(i, dataColumn);

                    //MessageBox.Show(dataPoint.YValue.ToString() + " y el maximo es " + matrix.getMax(dataColumn).ToString());


                    // Add dataPoint to DataPoints collection.
                    dataSeries.DataPoints.Add(dataPoint);
                }
            }

            // Add dataSeries to Series collection.
            chart.Series.Add(dataSeries);

            // Add chart to LayoutRoot
            //LayoutRoot.Children.Add(chart);

            */

            

            /**/
            //Background = Brushes.Yellow;
        }

        void chart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {

                chart.Height = Convert.ToInt16(chart.ActualHeight * 1.25);
                chart.Width = Convert.ToInt16(chart.ActualWidth * 1.25);

            }
        }

        void chart_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                (sender as Chart).Height = Convert.ToInt16((sender as Chart).ActualHeight * 1.25);
                (sender as Chart).Width = Convert.ToInt16((sender as Chart).ActualWidth * 1.25);
            }
            else
            {
                (sender as Chart).Height = Convert.ToInt16((sender as Chart).ActualHeight / 1.25);
                (sender as Chart).Width = Convert.ToInt16((sender as Chart).ActualWidth / 1.25);
            }
        }

        void chart_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                (sender as Chart).Height = Convert.ToInt16((sender as Chart).ActualHeight / 1.25);
                (sender as Chart).Width = Convert.ToInt16((sender as Chart).ActualWidth / 1.25);
            }
        }

        /*void chart_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            (sender as Chart).Height = Convert.ToInt16((sender as Chart).ActualHeight * 1.25);
            (sender as Chart).Width = Convert.ToInt16((sender as Chart).ActualWidth * 1.25);
        }*/



        void plot_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                
                //scrollingPane.Height = this.ActualHeight;
                config2.Height = Convert.ToInt16((sender as Image).ActualHeight / 1.25);
                (sender as Image).Height = Convert.ToInt16((sender as Image).ActualHeight / 1.25);
                (sender as Image).Width = Convert.ToInt16((sender as Image).ActualWidth / 1.25);
            }
        }

        void plot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount==2)
            {
                config2.Height = Convert.ToInt16((sender as Image).ActualHeight * 1.25);
                (sender as Image).Height = Convert.ToInt16((sender as Image).ActualHeight * 1.25);
                (sender as Image).Width = Convert.ToInt16((sender as Image).ActualWidth * 1.25);
            }

        }

        void plot_MouseWheel(object sender, MouseWheelEventArgs e)
        {

            if (e.Delta > 0)
            {
                config2.Height = Convert.ToInt16((sender as Image).ActualHeight * 1.25);
                (sender as Image).Height = Convert.ToInt16((sender as Image).ActualHeight * 1.25);
                (sender as Image).Width = Convert.ToInt16((sender as Image).ActualWidth * 1.25);
            }
            else
            {
                config2.Height = Convert.ToInt16((sender as Image).ActualHeight / 1.25);
                (sender as Image).Height = Convert.ToInt16((sender as Image).ActualHeight / 1.25);
                (sender as Image).Width = Convert.ToInt16((sender as Image).ActualWidth / 1.25);
            }
        }

        
        
        
        
        
        
        public Vis(Vis v)
        {
            //imagen = new Image();
            //imagen.Source = v.imagen.Source;
            dock = v.dock;
            //DockPanel.SetDock(imagen, Dock.Top);
            //v.RemoveContent(0);

            table = v.table;


            AvalonDock.DockableContent aux = new AvalonDock.DockableContent();
            //aux.Content = imagen;
            
            //v.AddChild(aux);
            AddChild(aux);
            //v.Children.Remove(imagen);
            //Children.Add(imagen);
            Background=Brushes.Yellow;


            AllowDrop = true;
            
            
        }

        public void CreateChart()
        {
            // Create a new instance of Chart
            Chart chart = new Chart();

            // Create a new instance of Title
            Title title = new Title();

            // Set title property
            title.Text = "Prueba";

            // Add title to Titles collection
            chart.Titles.Add(title);

            // Create a new instance of DataSeries
            DataSeries dataSeries = new DataSeries();

            // Set DataSeries property
            dataSeries.RenderAs = RenderAs.Column;

            // Create a DataPoint
            DataPoint dataPoint;

            for (int i = 0; i < 10; i++)
            {
                // Create a new instance of DataPoint
                dataPoint = new DataPoint();

                // Set YValue for a DataPoint
                //dataPoint.YValue = rand.Next(-100, 100);

                // Add dataPoint to DataPoints collection.
                dataSeries.DataPoints.Add(dataPoint);
            }

            // Add dataSeries to Series collection.
            chart.Series.Add(dataSeries);

            // Add chart to LayoutRoot
            //LayoutRoot.Children.Add(chart);
        }



        public void refresh(data matrix)
        {
            
            //MessageBox.Show("entra");
            /*for (int i=chart.Series.Count-1; i>=0; i--)
                chart.Series.Remove(dataSeries[i]);*/

            /*for (int i = 0; i < nSeries; i++)
            {
                int aux = dataSeries[i].DataPoints.Count();
                //MessageBox.Show(dataSeries.DataPoints.Count().ToString());
                while (dataSeries[i].DataPoints.Count() > 0)

                    dataSeries[i].DataPoints.Remove(dataSeries[i].DataPoints[0]);

            }*/

            //MessageBox.Show(dataSeries.DataPoints.Count().ToString());

            // Create a new instance of DataSeries
            //DataSeries dataSeries = new DataSeries();

            // Set DataSeries property
            //dataSeries.RenderAs = RenderAs.Column;

            // Create a DataPoint
            switch (type)
            {

                case 0:

                    bool rePaint = false;

                    int removed = 0;

                    int added = 0;

                    /*for (int i = 0; i < matrix.getnRows() && !rePaint; i++)
                    {
                        if (matrix.isInFilterRange[i])
                        //MessageBox.Show("Fila " + i.ToString());
                        rePaint = false;
                        if (showed[i] && !matrix.isInFilterRange[i])
                        {
                            //MessageBox.Show("Dejar de mostrar fila " + i.ToString());
                            for (int j = 0; j < nSeries; j++)
                                if (matrix.getData(i, groups[j]) == min || matrix.getData(i,groups[j]) == max)
                                    rePaint = true;
                            if (!rePaint)
                            {
                                bool finish = false;
                                for (int seriesCont = 0; seriesCont < nSeries; seriesCont++)
                                {
                                    
                                    
                                    for (int j = 0; j < nSteps && !finish; j++)
                                        if (matrix.getData(i, groups[seriesCont]) < steps[j])
                                        {
                                            int point;
                                            bool found = false;
                                            for (point = 0;int.Parse(dataSeries[seriesCont].DataPoints[point].XValue.ToString()) != j && point<dataSeries[seriesCont].DataPoints.Count; j++) ;
                                            if (point<dataSeries[seriesCont].DataPoints.Count)
                                                dataSeries[seriesCont].DataPoints[point].YValue--;
                                            else{
                                                DataPoint newPoint = new DataPoint();
                                                newPoint.XValue = j;
                                                newPoint.YValue = 1;
                                                dataSeries[seriesCont].DataPoints.Add(newPoint);
                                            }
                                        }
                                }
                                showed[i] = false;
                            }
                        }
                        if (!showed[i] && matrix.isInFilterRange[i])
                        {
                            for (int j = 0; j < nSeries; j++)
                                if (matrix.getData(i,groups[j]) < min || matrix.getData(i, groups[j]) > max)
                                    rePaint = true;
                            if (!rePaint)
                            {
                                bool finish = false;
                                for (int seriesCont = 0; seriesCont < nSeries; seriesCont++)
                                    for (int j = 0; j < nSteps && !finish; j++)
                                        if (matrix.getData(i, groups[seriesCont]) < steps[j])
                                        {
                                            int point;
                                            bool found = false;
                                            for (point = 0; int.Parse(dataSeries[seriesCont].DataPoints[point].XValue.ToString()) != j && point < dataSeries[seriesCont].DataPoints.Count; j++) ;
                                            if (point < dataSeries[seriesCont].DataPoints.Count)
                                                dataSeries[seriesCont].DataPoints[point].YValue++;
                                            else
                                            {
                                                DataPoint newPoint = new DataPoint();
                                                newPoint.XValue = j;
                                                newPoint.YValue = 1;
                                                dataSeries[seriesCont].DataPoints.Add(newPoint);
                                            }
                                        }
                                showed[i] = true;
                            }
                        }
                        
                    }*/

                    
                        

                            for (int j = chart.Series.Count - 1; j >= 0; j--)
                                chart.Series.Remove(dataSeries[j]);

                            for (int j = 0; j < nSeries; j++)
                            {
                                int aux = dataSeries[j].DataPoints.Count();
                                //MessageBox.Show(dataSeries.DataPoints.Count().ToString());
                                while (dataSeries[j].DataPoints.Count() > 0)

                                    dataSeries[j].DataPoints.Remove(dataSeries[j].DataPoints[0]);

                            }


                            dataSeries = new DataSeries[nSeries];

                            for (int i = 0; i < nSeries; i++)
                            {
                                dataSeries[i] = new DataSeries();
                            }

                            int nPoints = 0;
                            int[] nPointInSeries = new int[nSeries];
                            int[] nPointInSteps = new int[nSteps];



                            for (int j = 0; j < nSteps; j++)
                                nPointInSteps[j] = 0;

                            for (int j = 0; j < nSeries; j++)
                            {
                                nPointInSeries[j] = 0;
                            }
                            DataPoint[][] points = new DataPoint[nSeries][];
                            //MessageBox.Show(nSeries.ToString());
                            for (int series_cont = 0; series_cont < nSeries; series_cont++)
                            {
                                points[series_cont] = new DataPoint[matrix.getnRows()];

                                int classif = 0;

                                int group = 0;
                                int acum = 0;


                                for (int i = 0; i < matrix.getnRows(); i++)
                                {
                                    showed[i] = false;
                                    bool filt = true;

                                    if (matrix.isInFilterRange[i])
                                    {
                                        showed[i] = true;
                                        points[series_cont][nPointInSeries[series_cont]] = new DataPoint();
                                        //points[series_cont][nPointInSeries[series_cont]].XValue = matrix.getData(i, dataRow);
                                        points[series_cont][nPointInSeries[series_cont]].YValue = matrix.getData(table, i, groups[series_cont]);

                                        if (nPoints == 0)
                                        {
                                            min = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                            max = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                        }

                                        else
                                        {
                                            if (double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString()) < min)
                                                min = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                            if (double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString()) > max)
                                                max = double.Parse(points[series_cont][nPointInSeries[series_cont]].YValue.ToString());
                                        }



                                        nPointInSeries[series_cont]++;
                                        nPoints++;


                                    }

                                }

                            }



                            for (int i = 1; i <= nSteps; i++)
                            {
                                steps[i - 1] = min + ((max - min) / nSteps) * i;
                                if (i == 1)
                                    stepsLabels[i - 1] = i.ToString() + ": " + min.ToString() + " - " + steps[i - 1].ToString();
                                else
                                    stepsLabels[i - 1] = i.ToString() + ": " + steps[i - 2].ToString() + " - " + steps[i - 1].ToString();

                            }



                            steps[nSteps - 1] = max;

                            DataPoint[][] pointsToAdd = new DataPoint[nSeries][];

                            for (int i = 0; i < nSeries; i++)
                            {
                                pointsToAdd[i] = new DataPoint[nSteps];
                                for (int j = 0; j < nSteps; j++)
                                    pointsToAdd[i][j] = new DataPoint();
                            }



                            for (int i = 0; i < nSeries; i++)
                            {
                                for (int j = 0; j < nSteps; j++)
                                {
                                    pointsToAdd[i][j].AxisXLabel = stepsLabels[j];
                                    pointsToAdd[i][j].XValue = j;
                                    pointsToAdd[i][j].YValue = 0;
                                    nPointInSteps[j] = 0;
                                }

                                for (int j = 0; j < nPointInSeries[i]; j++)
                                {
                                    bool foundStep = false;
                                    int k;
                                    for (k = 0; !foundStep; k++)
                                        if (double.Parse(points[i][j].YValue.ToString()) <= steps[k])
                                            foundStep = true;

                                    k--;

                                    pointsToAdd[i][k].YValue++;

                                    nPointInSteps[k]++;

                                    //MessageBox.Show("Serie: " + j.ToString() + " step: " + k.ToString() + " PuntoY: " + points[i][j].YValue.ToString() + " npoints: " + nPointInSteps[k].ToString() + " Acumulado: "+pointsToAdd[i][k].YValue.ToString());
                                }

                                for (int j = 0; j < nSteps; j++)
                                {

                                    if (nPointInSteps[j] > 0)
                                    {

                                        //pointsToAdd[i][j].YValue = double.Parse(pointsToAdd[i][j].YValue.ToString()) / nPointInSteps[j];
                                        //MessageBox.Show("añado pto"+int.Parse(pointsToAdd[i][j].XValue.ToString())+","+int.Parse(pointsToAdd[i][j].YValue.ToString())+" a serie" + i);
                                        dataSeries[i].DataPoints.Add(pointsToAdd[i][j]);

                                    }
                                }

                                dataSeries[i].RenderAs = RenderAs.Column;
                                dataSeries[i].Name = matrix.getColumnTittle(table, groups[i]+1).Replace('-', '_');
                                chart.Series.Add(dataSeries[i]);
                            }
                        

                        break;

                case 1:
                        break;
                case 2:

                        for (int i = 0; i < matrix.getnRows(); i++)
                        {
                            if (groups[i] == 1)
                            {
                                if (matrix.isInFilterRange[i] && !showed[i])
                                {
                                    dataSeries[0].DataPoints.Add(dataPoints[i]);
                                    showed[i] = true;
                                }
                                if (!matrix.isInFilterRange[i] && showed[i])
                                {
                                    dataSeries[0].DataPoints.Remove(dataPoints[i]);
                                    showed[i] = false;
                                }
                            }
                        }


                        break;
                case 3:
                        break;
                case 4:
                        /*for (int j = chart.Series.Count - 1; j >= 0; j--)
                            chart.Series.Remove(dataSeries[j]);*/
                    
                        for (int j = 0; j < nSeries; j++)
                        {
                            int aux = dataSeries[j].DataPoints.Count();
                            //MessageBox.Show(dataSeries.DataPoints.Count().ToString());
                            
                            while (dataSeries[j].DataPoints.Count() > 0)

                                dataSeries[j].DataPoints.Remove(dataSeries[j].DataPoints[0]);

                        }
                        
                        for (int i = 0; i < nSeries; i++)
                        {

                            //MessageBox.Show("serie" + i.ToString());
                            int classif = 0;

                            int group = 0;

                            if (groups[i] != -1)
                            {

                                
                                
                                int acum = 0;

                                bool found_class = false;


                                for (int j = 0; !found_class; j++)
                                {
                                    if (groups[i] < acum + matrix.ngroups[j])
                                    {
                                        found_class = true;
                                        classif = j;
                                    }
                                    else
                                        acum = acum + matrix.ngroups[j];
                                }
                                group = groups[i] - (acum);

                                for (int j = 0; j < matrix.getnCols() - 1; j++)
                                {
                                    //MessageBox.Show("Serie " + i.ToString() + " columna " + j.ToString());
                                    if (matrix.groups[j][classif] == group)
                                    {
                                        DataPoint newPoint = new DataPoint();
                                        newPoint.XValue = j;
                                        newPoint.AxisXLabel = matrix.getColumnTittle(table,j + 1);
                                        double[] YValues = new double[matrix.getnRows()];
                                        int nValues = 0;
                                        for (int l = 0; l < matrix.getnRows(); l++)
                                            if (matrix.isInFilterRange[l])
                                            {
                                                YValues[nValues] = matrix.getData(table, l, j);
                                                for (int k = nValues; k > 0; k--)
                                                    if (YValues[k] < YValues[k - 1])
                                                    {
                                                        double a = YValues[k - 1];
                                                        YValues[k - 1] = YValues[k];
                                                        YValues[k] = a;
                                                    }
                                                nValues++;
                                                showed[l] = true;
                                            }
                                            else
                                                showed[i] = false;

                                        int per10 = nValues / 10;
                                        int per90 = (nValues * 90) / 100;
                                        int per25 = (nValues * 25) / 100;
                                        int per75 = (nValues * 75) / 100;

                                        double[] Y = new double[] { YValues[per25], YValues[per75], YValues[per10], YValues[per90] };

                                        newPoint.YValues = Y;
                                        dataSeries[i].DataPoints.Add(newPoint);
                                    }
                                }
                                /*dataSeries[i].RenderAs = RenderAs.CandleStick;
                                dataSeries[i].Name = "_"+matrix.groupsLabels[classif][group];
                                chart.Series.Add(dataSeries[i]);*/
                            }

                            else
                            {
                                for (int j = 0; j < matrix.getnCols() - 1; j++)
                                {

                                    DataPoint newPoint = new DataPoint();
                                    newPoint.XValue = j;
                                    newPoint.AxisXLabel = matrix.getColumnTittle(table, j + 1);
                                    double[] YValues = new double[matrix.getnRows()];
                                    int nValues = 0;
                                    for (int l = 0; l < matrix.getnRows(); l++)
                                        if (matrix.isInFilterRange[l])
                                        {
                                            YValues[nValues] = matrix.getData(table, l, j);
                                            //MessageBox.Show(matrix.getData(table,l,j).ToString());
                                            for (int k = nValues; k > 0; k--)
                                                if (YValues[k] < YValues[k - 1])
                                                {
                                                    double a = YValues[k - 1];
                                                    YValues[k - 1] = YValues[k];
                                                    YValues[k] = a;
                                                }
                                            nValues++;
                                            showed[l] = true;
                                        }
                                        else
                                            showed[i] = false;

                                    int per10 = nValues / 10;
                                    int per90 = (nValues * 90) / 100;
                                    int per25 = (nValues * 25) / 100;
                                    int per75 = (nValues * 75) / 100;

                                    double[] Y = new double[] { YValues[per25], YValues[per75], YValues[per10], YValues[per90] };

                                    newPoint.YValues = Y;
                                    dataSeries[i].DataPoints.Add(newPoint);

                                }

                                /*dataSeries[i].RenderAs = RenderAs.CandleStick;
                                dataSeries[i].Name = "All_Groups";
                                chart.Series.Add(dataSeries[i]);*/
                            }

                            


                        }
                        break;
                case 5:
                        for (int i = 0, rowsCont = 0; rowsCont < matrix.getnRows(); rowsCont++)
                        {
                            if (i < nSeries)
                            {
                                if (groups[i] == rowsCont)
                                {
                                    //MessageBox.Show(i.ToString());
                                    if (!showed[groups[i]] && matrix.isInFilterRange[groups[i]])
                                    {
                                        chart.Series.Add(dataSeries[groups[i]]);
                                        showed[groups[i]] = true;
                                    }
                                    if (showed[groups[i]] && !matrix.isInFilterRange[groups[i]])
                                    {
                                        chart.Series.Remove(dataSeries[groups[i]]);
                                        showed[groups[i]] = false;
                                    }

                                    i++;
                                }
                                else
                                {
                                    if (showed[rowsCont])
                                    {
                                        chart.Series.Remove(dataSeries[rowsCont]);
                                        showed[rowsCont] = false;
                                    }
                                }
                            }

                            else
                            {
                                if (showed[rowsCont])
                                {
                                    chart.Series.Remove(dataSeries[rowsCont]);
                                    showed[rowsCont] = false;
                                }
                            }

                        }
                        break;


            }
        }





                    
                    
                    
                    
            

            /*else
            {


                DataPoint dataPoint;


                for (int series_cont = 0; series_cont < nSeries; series_cont++)
                {

                    int classif = 0;

                    int group = -1;
                    int acum = 0;
                    if (groups[series_cont] != -1)
                    {
                        bool found_class = false;
                        for (int i = 0; !found_class; i++)
                        {
                            if (groups[series_cont] < acum + matrix.ngroups[i])
                            {
                                found_class = true;
                                classif = i;
                            }
                            else
                                acum = acum + matrix.ngroups[i];
                        }

                        group = groups[series_cont] - (acum);
                    }
                    //MessageBox.Show(classif.ToString() + " " + group.ToString());



                    dataSeries[series_cont] = new DataSeries();
                    dataSeries[series_cont].RenderAs = RenderAs.Column;
                    for (int i = 0; i < matrix.getnRows(); i++)
                    {
                        bool filt = true;
                        if (groups[series_cont] != -1)
                        {
                            if (matrix.groups[i][classif] != group)
                                filt = false;
                        }

                        for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                            if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                            {

                                //MessageBox.Show("En fila " + i.ToString() + " el dato 

                                filt = false;
                            }



                        //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))


                        if (filt)
                        {




                            // Create a new instance of DataPoint
                            dataPoint = new DataPoint();

                            // Set YValue for a DataPoint
                            dataPoint.YValue = matrix.getData(i, dataColumn);

                            dataPoint.XValue = matrix.getData(i, dataRow);

                            //MessageBox.Show(dataPoint.YValue.ToString() + " y el maximo es " + matrix.getMax(dataColumn).ToString());


                            // Add dataPoint to DataPoints collection.
                            dataSeries[series_cont].DataPoints.Add(dataPoint);
                            //MessageBox.Show("added row " + i.ToString() + " with value " + matrix.getData(i, dataColumn).ToString() + "," + matrix.getData(i, dataRow));
                        }
                    }
                    switch (type)
                    {
                        case 0:
                            
                            break;
                        case 2:
                            dataSeries[series_cont].RenderAs = RenderAs.Point;
                            break;
                        case 5:
                            dataSeries[series_cont].RenderAs = RenderAs.Line;
                            break;
                    }
                    chart.Series.Add(dataSeries[series_cont]);
                }
            }

            /*DataPoint dataPoint;

            for (int i = 0; i < matrix.getnRows(); i++)
            {
                bool filt = true;

                for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                    if (matrix.getData(i, j) < matrix.getMin(j) || matrix.getData(i, j) > matrix.getMax(j))
                    {

                        //MessageBox.Show("En fila " + i.ToString() + " el dato 

                        filt = false;
                    }

                if (filt)
                {
                    // Create a new instance of DataPoint
                    dataPoint = new DataPoint();

                    // Set YValue for a DataPoint
                    dataPoint.YValue = matrix.getData(i, dataColumn);

                    //MessageBox.Show(dataPoint.YValue.ToString() + " y el maximo es " + matrix.getMax(dataColumn).ToString());

                    //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))
                        // Add dataPoint to DataPoints collection.
                    dataSeries.DataPoints.Add(dataPoint);
                }
            }

            // Add dataSeries to Series collection.
            
            chart.Series.Add(dataSeries);

            //MessageBox.Show("sale");
        }*/
                    


        public void changeDock(Byte d)
        {
            dock = d;
        }

        public int getTableNumber()
        {
            return table;
        }

        public bool getCanceled()
        {
            return canceled;
        }

        public void click(data matrix, MouseButtonEventArgs e)
        {
                //MessageBox.Show();
            if (chart.PlotArea.IsMouseOver)
            {
                matrix.markingIndex[0] = dataColumn;
                matrix.markingIndex[1] = dataRow;
                MessageBox.Show("Hola");
                //matrix.markingMin[0] =


            }
        }

        

        void chartClicked(object sender, PlotAreaMouseButtonEventArgs e)
        {

                if (table == 0)
                {
                    switch (type)
                    {
                        case 0:
                            Window1.dat[table].markingIndex[0] = groups[0];
                            Window1.dat[table].markingIndex[1] = groups[0];
                            Window1.dat[table].markingMin[0] = double.Parse(e.XValue.ToString());
                            Window1.dat[table].markingMin[1] = double.Parse(e.XValue.ToString());

                            selected = true;
                            Window1.dat[table].ismarked = true;

                            break;
                        case 1:
                            break;
                        case 2:

                            Window1.dat[table].markingIndex[0] = dataRow;
                            Window1.dat[table].markingIndex[1] = dataColumn;
                            Window1.dat[table].markingMin[0] = double.Parse(e.XValue.ToString());
                            Window1.dat[table].markingMin[1] = double.Parse(e.YValue.ToString());

                            selected = true;
                            Window1.dat[table].ismarked = true;
                            break;
                        case 3:
                            break;
                        case 4:
                            Window1.dat[table].markingIndex[0] = Convert.ToInt32(double.Parse(e.XValue.ToString()));
                            Window1.dat[table].markingIndex[1] = Convert.ToInt32(double.Parse(e.XValue.ToString()));
                            Window1.dat[table].markingMin[0] = double.Parse(e.YValue.ToString());
                            Window1.dat[table].markingMin[1] = double.Parse(e.YValue.ToString());

                            selected = true;
                            break;


                        case 5:
                            Window1.dat[table].markingIndex[0] = Convert.ToInt32(double.Parse(e.XValue.ToString()));
                            Window1.dat[table].markingIndex[1] = Convert.ToInt32(double.Parse(e.XValue.ToString()));
                            Window1.dat[table].markingMin[0] = double.Parse(e.YValue.ToString());
                            Window1.dat[table].markingMin[1] = double.Parse(e.YValue.ToString());

                            selected = true;
                            break;
                    }
                }
            
            
        }

        void chartMouseLeave(object sender, MouseEventArgs e)
        {

            selected = false;
        }

        void chartClickedFinish(object sender, PlotAreaMouseButtonEventArgs e){

            if (selected){

                switch (type){
                    case 0:
                        if (double.Parse(e.XValue.ToString()) < (Window1.dat[table].markingMin[0]))
                        {
                            Window1.dat[table].markingMax[0] = Window1.dat[table].markingMin[0];
                            Window1.dat[table].markingMin[0] = double.Parse(e.XValue.ToString());
                        }
                        else
                            Window1.dat[table].markingMax[0] = double.Parse(e.XValue.ToString());
                        if (double.Parse(e.XValue.ToString()) < Window1.dat[table].markingMin[1])
                        {
                            Window1.dat[table].markingMax[1] = Window1.dat[table].markingMin[1];
                            Window1.dat[table].markingMin[1] = double.Parse(e.XValue.ToString());
                        }
                        else
                            Window1.dat[table].markingMax[1] = double.Parse(e.XValue.ToString());


                        if (normalized)
                        {
                            string[] labels;
                            string[] labels2;
                            string label;

                            bool foundStep = false;
                            int s;

                            for (s = 0; !foundStep; s++)
                            {
                                if (Window1.dat[table].markingMin[0] <= s)
                                {
                                    foundStep = true;
                                }
                            }

                            s--;

                            labels = stepsLabels[s].Split(':', '-');

                            label = labels[0].Trim();

                            Window1.dat[table].markingMin[0] = double.Parse(label);
                            Window1.dat[table].markingMin[1] = double.Parse(label);
                            foundStep = false;


                            for (s = nSteps - 1; !foundStep; s--)
                            {
                                if (Window1.dat[table].markingMax[0] >= s)
                                {
                                    foundStep = true;
                                }
                            }

                            s++;

                            labels2 = stepsLabels[s].Split(':', '-');

                            label = labels2[1].Trim();

                            Window1.dat[table].markingMax[0] = double.Parse(label);
                            Window1.dat[table].markingMax[1] = double.Parse(label);
                        }


                        break;
                    case 1:
                        break;
                    case 2:
                        if (double.Parse(e.XValue.ToString()) < Window1.dat[table].markingMin[0])
                        {
                            Window1.dat[table].markingMax[0] = Window1.dat[table].markingMin[0];
                            Window1.dat[table].markingMin[0] = double.Parse(e.XValue.ToString());
                        }
                        else
                            Window1.dat[table].markingMax[0] = double.Parse(e.XValue.ToString());
                        if (double.Parse(e.YValue.ToString()) < Window1.dat[table].markingMin[1])
                        {
                            Window1.dat[table].markingMax[1] = Window1.dat[table].markingMin[1];
                            Window1.dat[table].markingMin[1] = double.Parse(e.YValue.ToString());
                        }
                        else
                            Window1.dat[table].markingMax[1] = double.Parse(e.YValue.ToString());


                        if (normalized)
                        {
                            string[] labels;
                            string[] labels2;
                            string label;

                            bool foundStep = false;
                            int s;

                            for (s = 0; !foundStep; s++)
                            {
                                if (Window1.dat[table].markingMin[0] <= s)
                                {
                                    foundStep = true;
                                }
                            }

                            s--;

                            labels = stepsLabels[s].Split(':', '-');

                            label = labels[1].Trim();

                            Window1.dat[table].markingMin[0] = double.Parse(label);

                            foundStep = false;


                            for (s = nSteps - 1; !foundStep; s--)
                            {
                                if (Window1.dat[table].markingMax[0] >= s)
                                {
                                    foundStep = true;
                                }
                            }

                            s++;

                            labels2 = stepsLabels[s].Split(':', '-');

                            label = labels2[2].Trim();

                            Window1.dat[table].markingMax[0] = double.Parse(label);
                        }
                            break;

                    case 3:
                            break;
                    case 4:
                            if (double.Parse(e.YValue.ToString()) < Window1.dat[table].markingMin[0])
                            {
                                Window1.dat[table].markingMax[0] = Window1.dat[table].markingMin[0];
                                Window1.dat[table].markingMin[0] = double.Parse(e.YValue.ToString());
                            }
                            else
                                Window1.dat[table].markingMax[0] = double.Parse(e.YValue.ToString());
                            if (Convert.ToInt32(double.Parse(e.XValue.ToString())) < Window1.dat[table].markingIndex[0])
                            {
                                Window1.dat[table].markingIndex[1] = Window1.dat[table].markingIndex[0];
                                Window1.dat[table].markingIndex[0] = Convert.ToInt32(double.Parse(e.XValue.ToString()));
                            }
                            else
                                Window1.dat[table].markingIndex[1] = Convert.ToInt32(double.Parse(e.XValue.ToString()));

                            Window1.dat[0].markingMax[1] = Window1.dat[0].markingMax[0];
                            Window1.dat[0].markingMin[1] = Window1.dat[0].markingMin[0];
                            break;
                            
                    case 5:
                            if (double.Parse(e.YValue.ToString()) < Window1.dat[table].markingMin[0])
                            {
                                Window1.dat[table].markingMax[0] = Window1.dat[table].markingMin[0];
                                Window1.dat[table].markingMin[0] = double.Parse(e.YValue.ToString());
                            }
                            else
                                Window1.dat[table].markingMax[0] = double.Parse(e.YValue.ToString());
                            if (Convert.ToInt32(double.Parse(e.XValue.ToString())) < Window1.dat[table].markingIndex[0])
                            {
                                Window1.dat[table].markingIndex[1] = Window1.dat[table].markingIndex[0];
                                Window1.dat[table].markingIndex[0] = Convert.ToInt32(double.Parse(e.XValue.ToString()));
                            }
                            else
                                Window1.dat[table].markingIndex[1] = Convert.ToInt32(double.Parse(e.XValue.ToString()));

                            Window1.dat[0].markingMax[1] = Window1.dat[0].markingMax[0];
                            Window1.dat[0].markingMin[1] = Window1.dat[0].markingMin[0];
                            break;
                }
                        
                



                Window1.updateFilters(table);

                selected = false;
                
                //MessageBox.Show("Columna " + Window1.dat[table].markingIndex[0].ToString() + ": " + Window1.dat[table].markingMin[0].ToString() + "-" +Window1.dat[table].markingMax[0].ToString());
                //MessageBox.Show("Columna " + Window1.dat[table].markingIndex[1].ToString() + ": " + Window1.dat[table].markingMin[1].ToString() + "-" + Window1.dat[table].markingMax[1].ToString()); 
            }
        }



        private void changeTable(object sender, EventArgs e)
        {
            int table_ = 0;
            for (int i = 0; i <= combo.SelectedIndex; table_++)
                if (Window1.dat[0].isCreated(table_)) i++;
            table=table_-1;

            switch (type)
            {
                case 0:

                    this.refresh(Window1.dat[0]);

                    break;

                case 1:
                    break;

                case 2:
                    refreshScatterPlot(Window1.dat[0]);
                    break;

                case 4:
                    refresh(Window1.dat[0]);
                    break;

                case 5:
                    refreshLineChart(Window1.dat[0]);
                    
                    break;

                case 10:

                    showTable = new viewDataTable(Window1.dat[0].getTable(table));

                    config1.Children.Remove(config1.Children[1]);
                    config1.Children.Add(showTable);

                    break;



            }
        }

        private void but_Click(object sender, EventArgs e)
        {
            if (!(int.Parse(text.Text.ToString()) == nSteps))
            {
                nSteps = int.Parse(text.Text.ToString());

                steps = new double[nSteps];
                stepsLabels = new string[nSteps];
                normalizedSeries = new double[1][];



                //groups = new int[nseries];

                this.refresh(Window1.dat[0]);
            }



        }

        private void configureSeries_Click(object sender, EventArgs e)
        {

            switch (type)
            {

                case 0:

                    string[] aux1 = new string[Window1.dat[0].getnCols() - 1];

                    for (int i = 1; i < Window1.dat[0].getnCols(); i++)
                        aux1[i - 1] = Window1.dat[0].getColumnTittle(0, i);


                    Histogram_Design design = new Histogram_Design(aux1, Window1.dat[0].getnCols() - 1, Window1.dat[0].groupsLabels, Window1.dat[0].ngroups, Window1.dat[0].nClass);
                    if (design.ShowDialog().Value)
                    {
                        for (int j = chart.Series.Count - 1; j >= 0; j--)
                            chart.Series.Remove(dataSeries[j]);

                        nSeries = design.get_nSeries();
                        groups = new int[nSeries];

                        for (int i = 0; i < nSeries; i++)
                        {
                            groups[i] = design.get_Series(i);
                        }


                        dataSeries = new DataSeries[nSeries];

                        for (int i = 0; i < nSeries; i++)
                        {
                            dataSeries[i] = new DataSeries();
                        }



                        this.refresh(Window1.dat[0]);

                        
                    }

                    break;

                case 2:

                    LineChart_Design design_SP = new LineChart_Design(Window1.dat[0], table);

                    if (!design_SP.ShowDialog().Value)
                        break;

                    
                    groups = new int[Window1.dat[0].getnRows()];

                    for (int i = 0; i < Window1.dat[0].getnRows(); i++)
                    {
                        if (design_SP.isSelected(i))
                        {
                            groups[i] = 1;
                            //MessageBox.Show(i.ToString());
                        }
                        else
                            groups[i] = 0;
                    }

                    refreshScatterPlot(Window1.dat[0]);
                    break;

                case 5:

                    LineChart_Design design_LC = new LineChart_Design(Window1.dat[0], table);

                    if (!design_LC.ShowDialog().Value)
                        break;

                    nSeries = design_LC.get_nSeries();

                    if (nSeries > 1000)
                    {
                        MessageBox.Show("You selected " + nSeries.ToString() + " genes. Due to efficiency problems, only first 1000 selections will be added. This will be solved in our next release.");
                        nSeries = 1000;
                    }
                    //groups = new int[Window1.dat[0].getnRows()];
                    groups = new int[nSeries];
                    for (int i = 0,j=0; i < Window1.dat[0].getnRows()&&j<nSeries; i++)
                    {
                        if (design_LC.isSelected(i))
                        {
                            groups[j] = i;
                            j++;
                            //MessageBox.Show(i.ToString());
                        }
                        
                    }

                    refresh(Window1.dat[0]);
                    break;

                case 4:

                    BoxPlott_Design design_BP = new BoxPlott_Design(Window1.dat[0].groupsLabels, Window1.dat[0].ngroups, Window1.dat[0].nClass);
                    if (!design_BP.ShowDialog().Value)
                        break;

                    while (chart.Series.Count > 0)
                        chart.Series.Remove(chart.Series[0]);

                    nSeries = design_BP.get_nSeries();

                    groups = new int[nSeries];
                    dataSeries = new DataSeries[nSeries];

                    for (int i = 0; i < nSeries; i++)
                    {
                        groups[i] = design_BP.get_Series(i);
                        dataSeries[i] = new DataSeries();
                        dataSeries[i].Name = "_"+design_BP.getLabel(i);
                        dataSeries[i].RenderAs = RenderAs.CandleStick;
                        chart.Series.Add(dataSeries[i]);

                    }



                    

                    refresh(Window1.dat[0]);

                    break;


            }




        }

        public void refreshCombo(data matrix)
        {
            
            while (combo.Items.Count > 1)
                combo.Items.Remove(combo.Items[1]);

            for (int i = 1; i < 20; i++)
            {
                if (matrix.isCreated(i)&&i!=2)
                {
                    combo.Items.Add(matrix.getTableName(i));
                }
            }
        }

        public int getType()
        {
            return type;
        }

        private void changeAxys(object sender, SelectionChangedEventArgs e)
        {
            dataRow = comboX.SelectedIndex;
            dataColumn = comboY.SelectedIndex;
            refreshScatterPlot(Window1.dat[0]);
        }

        private void refreshScatterPlot(data matrix)
        {

            while (chart.Series.Count > 0)
                chart.Series.Remove(chart.Series[0]);

            DataPoint dataPoint = new DataPoint();

            for (int series_cont = 0; series_cont < nSeries; series_cont++)
            {

                int classif = 0;

                int group = 0;
                int acum = 0;

                bool found_class = false;
                dataSeries = new DataSeries[nSeries];
                dataSeries[series_cont] = new DataSeries();
                dataSeries[series_cont].RenderAs = RenderAs.Column;
                for (int i = 0; i < matrix.getnRows(); i++)
                {
                    dataPoints[i] = new DataPoint();
                    dataPoints[i].XValue = matrix.getData(table, i, dataRow);
                    dataPoints[i].YValue = matrix.getData(table, i, dataColumn);
                    
                    showed[i] = false;
                    bool filt = true;
                    if (groups[i] != 1)
                    {
                        filt = false;
                    }

                    for (int j = 0; j < matrix.getnCols() - 1 && filt; j++)

                        if (matrix.getData(0, i, j) < matrix.getMin(j) || matrix.getData(0, i, j) > matrix.getMax(j))
                        {

                            //MessageBox.Show("En fila " + i.ToString() + " columna " + j.ToString() + " el dato " + matrix.getData(0, i, j).ToString() + " y filtros " + matrix.getMin(j) + " - " + matrix.getMax(j));

                            filt = false;
                        }

                    //if (dataPoint.YValue <= matrix.getMax(dataColumn) && dataPoint.YValue >= matrix.getMin(dataColumn))


                    if (filt)
                    {

                        showed[i] = true;

                        // Add dataPoint to DataPoints collection.
                        dataSeries[series_cont].DataPoints.Add(dataPoints[i]);
                        //MessageBox.Show("added row " + i.ToString() + " with value " + matrix.getData(i, dataColumn).ToString() + "," + matrix.getData(i, dataRow));
                    }
                }

                dataSeries[series_cont].RenderAs = RenderAs.Point;

                chart.Series.Add(dataSeries[series_cont]);
            }
        }


        public int getNSeries()
        {

            return nSeries;
        }

        public void setNSeries(int i)
        {
            nSeries = i;
            //groups = new int[i];
        }

        public int getSerie(int i)
        {
            return groups[i];
        }

        public void setSerie(int i, int s)
        {
            groups[i] = s;
        }

        public int getNSteps()
        {
            return nSteps;
        }

        public void setNSteps(int steps)
        {
            nSteps = steps;
        }

        public void setTable(int t)
        {
            table = t;
        }

        private void ExecuteCommand(object sender, ExecutedRoutedEventArgs a)
        {
            //MessageBox.Show("entra");
            if (a.Command == CloseCommand)
                Window1.tabs[Window1.selectedTab].CloseVisualization(this);
                //((((this.Parent as AvalonDock.ResizingPanel).Parent as AvalonDock.DockingManager).Parent as ContentControl).Parent as Tab).CloseVisualization(this);

        }

        private void refreshLineChart(data matrix)
        {

            bool newCol = false;

            if (dataSeries[0].DataPoints.Count != matrix.getnRows())

                newCol = true;

            for (int i = 0; i < matrix.getnRows(); i++)

                for (int j = 0; j < matrix.getnCols() - 1; j++)

                    if (newCol && j == matrix.getnCols() - 2)
                    {
                        DataPoint point = new DataPoint();
                        point.AxisXLabel = matrix.getColumnTittle(table, j + 1);
                        point.YValue = matrix.getData(table, i, j);
                        dataSeries[i].DataPoints.Add(point);
                    }
                    else
                        
                        dataSeries[i].DataPoints[j].YValue = matrix.getData(table, i, j);

        }
       


    }
}
